library(plyr)
library(estimatr)
library(emmeans)
library(tidyverse)
library(magrittr)
library(broom)
library(modelsummary)
library(kableExtra)


d1 <- "https://github.com/thomasjwood/full_fact/raw/main/ff_mt.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS %>% 
  mutate(
    out_agree = out_agree %>% 
      mapvalues(
        c("Tend to disagree",
          "Tend to agree"),
        c("Disagree",
          "Agree")
      ) %>% 
      factor(
        c("Strongly disagree",
          "Disagree",
          "Neither agree nor disagree",
          "Agree",
          "Strongly agree")
      ),
    out_true = out_true %>% 
      factor(
        c("True",
          "Probably true",
          "Not sure",
          "Probably false",
          "False") %>% 
          rev
      ),
    agree_num = out_agree %>% 
      as.numeric,
    true_num = out_true %>% 
      as.numeric,
    cond = cond %>% 
      factor(
        c("cond_items",
          "cond_misinfo",
          "cond_corr")
      )
  )

cor(d1$agree_num, d1$true_num)

d1$com_num <- d1$agree_num %>% 
  add(
    d1$true_num
  ) %>% 
  divide_by(2)


# attrition check--keep only respondents who did both waves

d1 %<>% 
  left_join(
    d1 %>% 
      select(
        country, caseid, wave
      ) %>% 
      group_by(
        country, caseid, wave
      ) %>% 
      slice(1) %>% 
      group_by(
        country, caseid
      ) %>% 
      summarize(
        waves_done = wave %>% unique %>% length
      ) %>% 
      filter(
        waves_done == 2
      )
  ) %>% 
  filter(
    waves_done %>% 
      is.na %>% 
      not
  ) %>% 
  select(-waves_done)

t1 <- d1 %>% 
  mutate(
    country = issue %>%
      str_detect(
        "global|saltwater"
      ) %>%
      ifelse(
        "Multi-country",
        country
      )
  ) %>%
  group_by(
    country, wave, issue
  ) %>% 
  nest 

t1$mods <- t1$data %>% 
  map(
    ~lm_robust(
      com_num ~ cond, 
      data = .x
    )
  )

t1$emm <- t1$mods %>% 
  map2(
    t1$data,
    ~emmeans(
      .x, 
      consec ~ cond, 
      data = .y
    )
  )

t1$edf <- t1$emm %>% 
  map(
    ~extract2(., 1) %>% 
      tidy
  )

t1$cont <- t1$emm %>% 
  map(
    ~extract2(., 2) %>% 
      tidy
  )

l_m <- t1 %>% 
  filter(
    wave == "wave 1" &
      country %>% 
      is_in(
        c("Argentina", "South Africa")
      )
  ) %>% 
  mutate(
    country = country %>% 
      factor(
        # c("Argentina", "South Africa", "United Kingdom", "Multi-country")
        c("Argentina", "South Africa")
      )
  ) %>% 
  arrange(country) %>% 
  use_series(mods)


l_m %<>% 
  set_attr(
    "names",
    t1 %>% 
      filter(
        wave == "wave 1" &
          country %>% 
          is_in(
            c("Argentina", "South Africa")
          )
      ) %>% 
      mutate(
        country = country %>% 
          factor(
            c("Argentina", "South Africa")
          )
      ) %>% 
      arrange(country) %>% 
      use_series(issue) %>% 
      str_replace_all("_", " ") %>% 
      str_to_title %>% 
      str_remove_all("Safrica ")
  )


l_m %>% 
  modelsummary(
    coef_rename = c(
      "condcond_misinfo"= "Cond(Misinfo)",
      "condcond_corr"   = "Cond(Correction)"
    ),
    stars =  T,
    # output = "latex",
    gof_omit =  "R2 Adj.|se_type",
    coef_omit = "(Intercept)",  
    fmt = "%.2f"
  ) %>% 
  add_header_above(
    c(" " =  1,
      "Argentina" = 5,
      "South Africa" = 5)
    # "United Kingdom" = 5,
    # "Multi-Country" = 2)
  )

# wave 1 uk & multi-country

l_m2 <- t1 %>% 
  filter(
    wave == "wave 1" &
      country %>% 
      is_in(
        c("United Kingdom", "Multi-country")
      )
  ) %>% 
  mutate(
    country = country %>% 
      factor(
        c("United Kingdom", "Multi-country")
        # c("Argentina", "South Africa")
      )
  ) %>% 
  filter(wave == "wave 1") %>% 
  arrange(country) %>% 
  use_series(mods)


l_m2 %<>% 
  set_attr(
    "names",
    t1 %>% 
      filter(
        wave == "wave 1" &
          country %>% 
          is_in(
            c("United Kingdom", "Multi-country")
          )
      ) %>% 
      mutate(
        country = country %>% 
          factor(
            c("United Kingdom", "Multi-country")
          )
      ) %>% 
      filter(wave == "wave 1") %>% 
      arrange(country) %>% 
      use_series(issue) %>% 
      str_replace_all("_", " ") %>% 
      str_to_title %>% 
      str_remove_all("Safrica ")
  )


l_m2 %>% 
  modelsummary(
    coef_rename = c(
      "condcond_misinfo"= "Cond(Misinfo)",
      "condcond_corr"   = "Cond(Correction)"
    ),
    stars =  T,
    # output = "latex",
    gof_omit =  "R2 Adj.|se_type",
    coef_omit = "(Intercept)",  
    fmt = "%.2f"
  ) %>% 
  add_header_above(
    c(" " =  1,
      "United Kingdom" = 5,
      "Multi-Country" = 2
    )
  )


# Wave 2, Argentina and South Africa

l_m3 <- t1 %>% 
  filter(
    wave == "wave 2" &
      country %>% 
      is_in(
        c("Argentina", "South Africa")
      )
  ) %>% 
  mutate(
    country = country %>% 
      factor(
        # c("United Kingdom", "Multi-country")
        c("Argentina", "South Africa")
      )
  ) %>% 
  arrange(country) %>% 
  use_series(mods)


l_m3 %<>% 
  set_attr(
    "names",
    t1 %>% 
      filter(
        wave == "wave 2" &
          country %>% 
          is_in(
            c("Argentina", "South Africa")
          )
      ) %>% 
      mutate(
        country = country %>% 
          factor(
            c("Argentina", "South Africa")
          )
      ) %>% 
      filter(wave == "wave 2") %>% 
      arrange(country) %>% 
      use_series(issue) %>% 
      str_replace_all("_", " ") %>% 
      str_to_title %>% 
      str_remove_all("Safrica ")
  )

l_m3 %>% 
  modelsummary(
    coef_rename = c(
      "condcond_misinfo"= "Cond(Misinfo)",
      "condcond_corr"   = "Cond(Correction)"
    ),
    stars =  T,
    # output = "latex",
    gof_omit =  "R2 Adj.|se_type",
    coef_omit = "(Intercept)",  
    fmt = "%.2f"
  ) %>% 
  add_header_above(
    c(" " =  1,
      "Argentina" = 5,
      "South Africa" = 5
    )
  )

# wave 2, UK & multi-country

l_m4 <- t1 %>% 
  filter(
    wave == "wave 2" &
      country %>% 
      is_in(
        c("United Kingdom", "Multi-country")
      )
  ) %>% 
  mutate(
    country = country %>% 
      factor(
        c("United Kingdom", "Multi-country")
        # c("Argentina", "South Africa")
      )
  ) %>% 
  arrange(country) %>% 
  use_series(mods)


l_m4 %<>% 
  set_attr(
    "names",
    t1 %>% 
      filter(
        wave == "wave 2" &
          country %>% 
          is_in(
            c("United Kingdom", "Multi-country")
          )
      ) %>% 
      mutate(
        country = country %>% 
          factor(
            c("United Kingdom", "Multi-country")
          )
      ) %>% 
      filter(wave == "wave 2") %>% 
      arrange(country) %>% 
      use_series(issue) %>% 
      str_replace_all("_", " ") %>% 
      str_to_title %>% 
      str_remove_all("Safrica ")
  )

l_m4 %>% 
  modelsummary(
    coef_rename = c(
      "condcond_misinfo"= "Cond(Misinfo)",
      "condcond_corr"   = "Cond(Correction)"
    ),
    stars =  T,
    # output = "latex",
    gof_omit =  "R2 Adj.|se_type",
    coef_omit = "(Intercept)",  
    fmt = "%.2f"
  ) %>% 
  add_header_above(
    c(" " =  1,
      "United Kingdom" = 5,
      "Multi Country" = 2
    )
  )
